//监听滚动条滚动方向
import { onBeforeUnmount, onMounted, ref } from "vue";

export default function authorScroll() {
  //滚动初始值
  let scrollOld = ref(0);
  const isNavbar = ref(true);
  onMounted(() => {
    // document.documentElement.scrollTop = 0;
    document.addEventListener("scroll", windowScroll, false);
  });

  const windowScroll = () => {
    // 滚动条距离页面顶部的距离
    let scrollTop = document.documentElement.scrollTop;
    let scroll = scrollTop - scrollOld.value;
    scrollOld.value = scrollTop;
    if (scroll <= 0) {
      isNavbar.value = true;
    } else {
      isNavbar.value = false;
    }
  };
  onBeforeUnmount(() => {
    document.removeEventListener("scroll", windowScroll);
  });
  return { isNavbar, scrollOld };
}
